home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / stenciltst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.9 KB  |  148 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <GL/glut.h>
  45.  
  46. GLboolean doubleBuffer;
  47.  
  48. /* ARGSUSED1 */
  49. static void
  50. Key(unsigned char key, int x, int y)
  51. {
  52.   switch (key) {
  53.   case 27:
  54.     exit(0);
  55.   }
  56. }
  57.  
  58. static void
  59. Draw(void)
  60. {
  61.   glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  62.  
  63.   glStencilFunc(GL_ALWAYS, 1, 1);
  64.   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  65.  
  66.   /* red triangle */
  67.   glColor3ub(200, 0, 0);
  68.   glBegin(GL_POLYGON);
  69.   glVertex3i(-4, -4, 0);
  70.   glVertex3i(4, -4, 0);
  71.   glVertex3i(0, 4, 0);
  72.   glEnd();
  73.  
  74.   glStencilFunc(GL_EQUAL, 1, 1);
  75.   glStencilOp(GL_INCR, GL_KEEP, GL_DECR);
  76.  
  77.   /* green square */
  78.   glColor3ub(0, 200, 0);
  79.   glBegin(GL_POLYGON);
  80.   glVertex3i(3, 3, 0);
  81.   glVertex3i(-3, 3, 0);
  82.   glVertex3i(-3, -3, 0);
  83.   glVertex3i(3, -3, 0);
  84.   glEnd();
  85.  
  86.   glStencilFunc(GL_EQUAL, 1, 1);
  87.   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  88.  
  89.   /* blue square */
  90.   glColor3ub(0, 0, 200);
  91.   glBegin(GL_POLYGON);
  92.   glVertex3i(3, 3, 0);
  93.   glVertex3i(-3, 3, 0);
  94.   glVertex3i(-3, -3, 0);
  95.   glVertex3i(3, -3, 0);
  96.   glEnd();
  97.  
  98.   if (doubleBuffer) {
  99.     glutSwapBuffers();
  100.   } else {
  101.     glFlush();
  102.   }
  103. }
  104.  
  105. static void
  106. Args(int argc, char **argv)
  107. {
  108.   GLint i;
  109.  
  110.   doubleBuffer = GL_TRUE;
  111.   for (i = 1; i < argc; i++) {
  112.     if (strcmp(argv[i], "-sb") == 0) {
  113.       doubleBuffer = GL_FALSE;
  114.     } else if (strcmp(argv[i], "-db") == 0) {
  115.       doubleBuffer = GL_TRUE;
  116.     }
  117.   }
  118. }
  119.  
  120. int
  121. main(int argc, char **argv)
  122. {
  123.   GLenum type;
  124.  
  125.   glutInit(&argc, argv);
  126.   Args(argc, argv);
  127.  
  128.   type = GLUT_RGB | GLUT_STENCIL;
  129.   type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  130.   glutInitDisplayMode(type);
  131.   glutCreateWindow("Stencil Test");
  132.  
  133.   glClearColor(0.0, 0.0, 0.0, 0.0);
  134.   glClearStencil(0);
  135.   glStencilMask(1);
  136.   glEnable(GL_STENCIL_TEST);
  137.  
  138.   glMatrixMode(GL_PROJECTION);
  139.   glLoadIdentity();
  140.   glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
  141.   glMatrixMode(GL_MODELVIEW);
  142.  
  143.   glutKeyboardFunc(Key);
  144.   glutDisplayFunc(Draw);
  145.   glutMainLoop();
  146.   return 0;             /* ANSI C requires main to return int. */
  147. }
  148.